package com.zhao.controller;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.zhao.pojo.Admin;
import com.zhao.pojo.Superviser;
import com.zhao.service.SuperviserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class SuperviserController {

    @Autowired
    private SuperviserService superviserService;



    @RequestMapping("admin-getAllSuperviser.do")
    private String adminGetAllSuperviser(HttpSession session){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = superviserService.getAll(admin.getUniversityID(),session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-allSupervisers";
    }

    @ResponseBody
    @RequestMapping("admin-addOneSuperviser.do")
    public String addOneSuperviser(String superviserID, String password, HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        Superviser superviser = new Superviser();
        superviser.setUniversityID(admin.getUniversityID());
        superviser.setSuperviserID(superviserID);
        if(password.equals("")){
            superviser.setPassword("123");
        }else {
            superviser.setPassword(password);
        }

        String res = superviserService.addOneSuperviser(superviser,session);
        System.out.println(res);

        return res;
    }

}
